package com.zpz.framework.zpzoauth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthBeanUtils;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthPageInfoExt;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthSnowflakeIdWorker;
import com.zpz.framework.zpzoauth.dao.ZpzOauthFrameRoleMapper;
import com.zpz.framework.zpzoauth.dao.ZpzOauthFrameUserRoleMapper;
import com.zpz.framework.zpzoauth.pojo.po.FrameRole;
import com.zpz.framework.zpzoauth.pojo.po.FrameUserRole;
import com.zpz.framework.zpzoauth.pojo.qo.*;
import com.zpz.framework.zpzoauth.pojo.vo.AddRoleInfoQo;
import com.zpz.framework.zpzoauth.pojo.vo.GetRoleInfoVo;
import com.zpz.framework.zpzoauth.pojo.vo.RoleDropdownListVo;
import com.zpz.framework.zpzoauth.pojo.vo.RoleManageVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangpanzhi
 * @description 角色管理服务实现
 * @create 2019-07-04 19:29
 */
@Service
public class ZpzOauthFrameRoleServiceImpl implements ZpzOauthFrameRoleService {

    @Resource
    private ZpzOauthFrameRoleMapper zpzOauthFrameRoleMapper;

    @Resource
    private ZpzOauthFrameUserRoleMapper zpzOauthFrameUserRoleMapper;

    /**
     * 获取当前用户角色列表
     *
     * @param
     * @return
     */
    @Override
    public ZpzOauthResultMsg<List<String>> getRoleCodeByUserCode(HttpServletRequest request, GetRoleCodeByUserCodeListQo getRoleCodeByUserCodeListQo) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (ucodeLicense == null) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        String ucode = getRoleCodeByUserCodeListQo.getUserCode();
        if (StringUtils.isBlank(ucode) || StringUtils.isBlank(getRoleCodeByUserCodeListQo.getClientId()) || !ucode.equals(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_INFO_SELETE_FAILED_38012);
        }

        List<String> list = zpzOauthFrameRoleMapper.selectRoleByClientAndUser(getRoleCodeByUserCodeListQo.getClientId(), ucode);
        if (list == null || list.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CUR_SELETE_ISNULL_38013);
        }
        return ZpzOauthResultMsg.okForData(list);
    }

    /**
     * 新增角色信息
     *
     * @param
     * @return
     */
    @Override
    public ZpzOauthResultMsg<Integer> addRole(HttpServletRequest request, AddRoleInfoQo addRoleInfoQo) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        String clientIdLincese = license.get("clientId");
        if (StringUtils.isBlank(addRoleInfoQo.getClientId())) {
            if (StringUtils.isBlank(ucodeLicense)) {
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10026);
            } else {
                addRoleInfoQo.setClientId(clientIdLincese);
            }
        }

        FrameRole existRole = zpzOauthFrameRoleMapper.checkRoleName(addRoleInfoQo.getRoleName());
        if (existRole != null) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_NAME_EXIST_38001);
        }

        FrameRole role = new FrameRole();
        ZpzOauthSnowflakeIdWorker worker = new ZpzOauthSnowflakeIdWorker(2, 1);
        role.setClientId(addRoleInfoQo.getClientId());
        role.setRoleName(addRoleInfoQo.getRoleName());
        role.setRoleCode(worker.nextId() + "");
        role.setRoleDesc(addRoleInfoQo.getRoleDesc());
        role.setType(2);
        role.setIsAdmin(0);
        role.setStatus("1");
        role.setCreateBy(ucodeLicense);
        role.setCreateDate(new Date(System.currentTimeMillis()));
        role.setDelFlag(0);
        int insertResult = zpzOauthFrameRoleMapper.insert(role);
        if (insertResult > 0) {
            return ZpzOauthResultMsg.okForData(role.getId());
        } else {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_ADD_FAILED_38003);
        }
    }

    /**
     * 编辑角色信息
     *
     * @param
     * @return
     */
    @Override
    public ZpzOauthResultMsg<Integer> editRole(HttpServletRequest request, ModifyRoleInfoQo modifyRoleInfoQo) {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        String clientIdLincese = license.get("clientId");
        if (StringUtils.isBlank(modifyRoleInfoQo.getClientId())) {
            if (StringUtils.isBlank(ucodeLicense)) {
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10026);
            } else {
                modifyRoleInfoQo.setClientId(clientIdLincese);
            }
        }


        FrameRole existRole = zpzOauthFrameRoleMapper.checkRoleName(modifyRoleInfoQo.getRoleName());
        if (existRole != null && !existRole.getRoleCode().equals(modifyRoleInfoQo.getRoleCode())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_NAME_EXIST_38001);
        }
        FrameRole role = new FrameRole();
        role.setClientId(modifyRoleInfoQo.getClientId());
        role.setRoleCode(modifyRoleInfoQo.getRoleCode());
        role.setRemarks(modifyRoleInfoQo.getRemarks());
        role.setRoleName(modifyRoleInfoQo.getRoleName());
        role.setRoleDesc(modifyRoleInfoQo.getRoleDesc());
        role.setStatus(String.valueOf(modifyRoleInfoQo.getStatus()));
        role.setUpdateBy(ucodeLicense);
        role.setUpdateDate(new Date(System.currentTimeMillis()));
        int updateResult = zpzOauthFrameRoleMapper.modifyRole(role);
        if (updateResult > 0) {
            return ZpzOauthResultMsg.okForData(updateResult);
        } else {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_UPDATE_FAILED_38004);
        }
    }

    /**
     * @throws Exception
     * @name: getRoleDropdownList
     * @description:获取角色下拉列表信息
     * @returnType: java.util.List<com.auyan.framework.users.model.vo.RoleDropdownListVo>
     * @createBy: changyifan
     * @createDate: 2019-05-16 05:41:48
     * @updateBy:
     * @updateDate:
     * @param: []
     */
    @Override
    public ZpzOauthResultMsg<List<RoleDropdownListVo>> getRoleDropdownList(HttpServletRequest request, GetClientQo getClientQo) throws Exception {
        if (StringUtils.isBlank(getClientQo.getClientId())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        List<FrameRole> list = zpzOauthFrameRoleMapper.selectRoleDropdownList(getClientQo.getClientId());
        if (list != null) {
            List<RoleDropdownListVo> listVos = ZpzOauthBeanUtils.copyListProperties(list, RoleDropdownListVo.class);
            return ZpzOauthResultMsg.okForData(listVos);
        }
        return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_INFO_FAILED_38010);
    }

    /**
     * @throws Exception
     * @name: uopdateRoleState
     * @description:修改角色启用状态
     * @returnType: com.auyan.framework.common.result.ResultMsg<java.lang.Boolean>
     * @createBy: changyifan
     * @createDate: 2019-05-17 02:00:45
     * @updateBy:
     * @updateDate:
     * @param: [roleStateQo]
     */
    @Override
    public ZpzOauthResultMsg<Boolean> updateRoleState(HttpServletRequest request, RoleStateQo roleStateQo) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        FrameRole frameRole = new FrameRole();
        frameRole.setRoleCode(roleStateQo.getRoleCode());
        frameRole.setStatus(roleStateQo.getStatus());
        frameRole.setUpdateBy(ucodeLicense);
        frameRole.setUpdateDate(new Date(System.currentTimeMillis()));
        Boolean b = zpzOauthFrameRoleMapper.updateRoleState(frameRole);
        if (b == true) {
            return ZpzOauthResultMsg.okForData(b);
        } else {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_STATE_UPDATE_FAILED_38011);
        }
    }


    /**
     * @param roleManageQo
     * @return com.github.pagehelper.PageInfo<com.auyan.frxamework.users.model.vo.RoleManageVo>
     * @Author fengxm
     * @NAME 查询角色管理列表
     * @Description //TODO
     * @Date 2019/5/17 14:11
     */
    @Override
    public ZpzOauthResultMsg<PageInfo<RoleManageVo>> selectRoleMangePageList(HttpServletRequest request, RoleManageQo roleManageQo) {
        if (StringUtils.isBlank(roleManageQo.getClientId())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }

        PageHelper.startPage(roleManageQo.getPageNum(), roleManageQo.getPageSize(), true);
        Map map = new HashMap();
        map.put("clientId", roleManageQo.getClientId());
        map.put("roleName", roleManageQo.getRoleName());
        List<FrameRole> roleManageList = zpzOauthFrameRoleMapper.selectRoleMangePageList(map);
        if (roleManageList == null) {
            ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_INFO_SELETE_FAILED_38012);
        }
        PageInfo<RoleManageVo> page = new ZpzOauthPageInfoExt<FrameRole, RoleManageVo>().getPage(roleManageList, RoleManageVo.class);
        return ZpzOauthResultMsg.okForData(page);
    }

    /**
     * @param deleteRoleInfoQo
     * @return com.auyan.framework.common.result.ResultMsg<java.lang.Boolean>
     * @Author fengxm
     * @NAME 删除角色相关信息
     * @Description //TODO
     * @Date 2019/5/17 17:07
     */
    @Override
    public ZpzOauthResultMsg<Boolean> deleteRoleInfo(HttpServletRequest request, DeleteRoleInfoQo deleteRoleInfoQo) {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }

        //验证该角色是否已绑定用户
        FrameUserRole frameUserRole = new FrameUserRole();
        frameUserRole.setDelFlag(0);
        frameUserRole.setRoleCode(deleteRoleInfoQo.getRoleCode());
        int count = zpzOauthFrameUserRoleMapper.selectCount(frameUserRole);
        if (count > 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_INFO_DELETE_FAILED_38016);
        }

        FrameRole frameRole = new FrameRole();
        BeanUtils.copyProperties(deleteRoleInfoQo, frameRole);
        Integer deleteRole = zpzOauthFrameRoleMapper.deleteRoleInfo(frameRole);
        if (deleteRole > 0) {
            return ZpzOauthResultMsg.okForData(true);
        }
        return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_INFO_DELETE_FAILED_38009);
    }

    @Override
    public ZpzOauthResultMsg<GetRoleInfoVo> getRoleInfoByCode(String roleCode) {
        GetRoleInfoVo roleInfoVo = new GetRoleInfoVo();
        FrameRole role = zpzOauthFrameRoleMapper.getRoleByRoleCode(roleCode);
        if (role != null) {
            roleInfoVo.setRoleCode(role.getRoleCode());
            roleInfoVo.setRoleName(role.getRoleName());
            roleInfoVo.setRoleDesc(role.getRoleDesc());
            return ZpzOauthResultMsg.okForData(roleInfoVo);
        } else {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }
    }
}
